package com.jlastu.dlxy_androidapp.view.main;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.base.BaseFragment;
import com.jlastu.dlxy_androidapp.contract.MineContract;
import com.jlastu.dlxy_androidapp.entity.User;
import com.jlastu.dlxy_androidapp.presenter.MinePresenter;
import com.jlastu.dlxy_androidapp.utils.SpUtils;


public class MineFragment extends BaseFragment implements View.OnClickListener, MineContract.IMineView {

   private TextView mTvName;
   private RelativeLayout mSettingLayout,mOtherView;
   private MineContract.IMinePresenter mMinePresenter;
   private ImageView mShoppingLayout;

   private ImageView mProfileImageView;
   private TextView mUserUpdate;
   private SpUtils mSpUtils;

   @Override
   protected int getLayoutId() {
      return R.layout.fragment_mine;
   }

   @Override
   protected void initView() {
      mMinePresenter = new MinePresenter(this, getContext());
      mSettingLayout = mView.findViewById(R.id.settingView);
      mShoppingLayout = mView.findViewById(R.id.shoppingCartImageView);
      mTvName = mView.findViewById(R.id.usernameTextView);
      mUserUpdate = mView.findViewById(R.id.userupdateTextView);
      mProfileImageView = mView.findViewById(R.id.profileImageView);
      mSpUtils = new SpUtils(mContext);

      mUserUpdate.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getActivity(), MineDetailsActivity.class);
            startActivity(intent);
         }
      });

//购物车界面
      mShoppingLayout.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getActivity(), ShoppingCartActivity.class);
            startActivity(intent);
         }
      });

      //我的订单点击事件
      mOtherView = mView.findViewById(R.id.otherView);
      mOtherView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getActivity(),MyOrdersActivity.class);
            startActivity(intent);
         }
      });
   }

   @Override
   public void onResume() {
      super.onResume();
      //查询当前登录帐号的id
      int user_id = mSpUtils.getInt("userid", 0);
      if (user_id >= 0) {
         //根据id查询当前登录用户的信息
         mMinePresenter.getLoginUser(user_id);
      }
   }

   @Override
   protected void initListener() {
      mSettingLayout.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.settingView) {
         Intent intent = new Intent(getActivity(), SettingsActivity.class);
         startActivity(intent);
      }
   }


   @Override
   public void showUserInfo(User user) {
      byte[] byteArray;
      if (mTvName != null && user != null) {
         mTvName.setText(user.getUserName());
         mProfileImageView.setImageBitmap(user.getBitmap());
      }
      if (user != null && user.getData() != null) {
         byteArray = user.getData();
         Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
         mProfileImageView.setImageBitmap(bitmap);
      }else {
         //没有查到图片显示默认图片
         mProfileImageView.setImageResource(R.mipmap.ic_launcher);
      }
   }


}
